﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Script.Services;

namespace Sinacor.Infra.Tools.ServiceRegistration.Web
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class AutoComplete : WebService
    {
        public AutoComplete()
        {
        }

        private List<Entity.Type> _typeList;

        [WebMethod]
        public string[] GetCompletionList(string prefixText, int count)
        {
            if (System.Web.HttpContext.Current.Application["TypeList"] != null)
                _typeList = (List<Entity.Type>)System.Web.HttpContext.Current.Application["TypeList"];
            
            Business.Type biz = new Business.Type();
            IEnumerable<string> items;

            if (_typeList == null)
            {
                _typeList = biz.SelectType(new Entity.Type());
                System.Web.HttpContext.Current.Application["TypeList"] = _typeList;
            }

            items = from n in _typeList
                    where n.FullTypeName.ToLower().Contains(prefixText.ToLower())
                    where n.ParentTypeId == 0
                    select n.FullTypeName;

            return items.ToArray();
        }
    }
}